package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import com.tensquare.tensquare_common.entity.Result;
import com.tensquare.tensquare_common.entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhc
 * @create 2019-04-18  14:04
 */
@RestController
@RequestMapping("/friend")
@CrossOrigin
public class FriendController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private FriendService friendService;

    @PutMapping("/like/{friendid}/{type}")
    public Result addFriend(@PathVariable String friendid,@PathVariable String type){
        //验证是否登录，并且拿到用户id
        Claims claims= (Claims) request.getAttribute("claims_user");
        System.out.println(claims.getId()+"=========");
        if(claims==null||"".equals(claims)){
            //没有角色
            return new Result(false,StatusCode.ACCESSERROR,"没有权限");
        }
        //得到当前用户的id
        String userid = claims.getId();
        userid="1118790950936776704";
        //判断是添加好友还是添加非好友
        if(type!=null){
            if(type.equals("1")){
                //添加好友
                int flag = friendService.addFriend(userid, friendid);
                if(flag==0){
                    return new Result(false,StatusCode.ERROR,"不能重复添加好友");
                }
                if(flag==1){
                    return new Result(true,StatusCode.OK,"添加好友成功");
                }
            }else if(type.equals("2")){
                //添加非好友
            }else{
                return new Result(false, StatusCode.ERROR,"参数异常");
            }
        }
        return new Result(false, StatusCode.ERROR,"参数异常");
    }
}
